<?php
/**
 * 入职流程批处理
 * ***********
 * 2013-02-17
 * 增加取消入职流程办理
 * *增加新阶段4，
 * *取消前，行政或IT已经办理，则可以执行取消办理操作
 * *同时处理入职流程部分 AddStaff
 */
apf_require_controller("Abstract");
class Flow_DoListController extends AbstractController {
    private $error_item;//验证错误项
    public function handle_request() {
        $params = $this->request->get_parameters();
        $role_list = $this->user_info['role_list'];
        $flow_ids = $params['flow_ids'];
        if($params['type']== 1 ){//入职
            $this->_entry_ok($flow_ids);
            $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
        }elseif($params['type']== 2 ){//离职
            $this->_entry_cancel($flow_ids);
            $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
        }elseif($params['type']== 3 ){//办理
            $users = array();
            //it&xz
            if(isset($role_list[Const_Flow::ROLE_ADMIN]) && isset($role_list[Const_Flow::ROLE_IT])){
                if(isset($params['role']) && $params['role']=='xz'){//xz 提交
                    if($this->_check_post_xz($params)){
                        $users = $this->_build_post_xz($params);
                        $this->_execute_xz($users);
                        $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
                    }else{
                        $users = $this->_build_post_xz_error($params);
                    }
                }else{
                    //先执行it
                    $flow_entry = $this->_execute_it_or_xz($flow_ids);
                    $this->_execute_it($flow_entry['it']);
                    //再执行行政
                    $users = $this->_get_user_info($flow_entry['xz']);
                    if(empty($users)){
                        $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
                    }
                }
            }elseif(isset($role_list[Const_Flow::ROLE_ADMIN])){ //行政
                if(isset($params['role']) && $params['role']=='xz'){//xz 提交
                    if($this->_check_post_xz($params)){
                        $users = $this->_build_post_xz($params);
                        $this->_execute_xz($users);
                        $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
                    }else{
                        $users = $this->_build_post_xz_error($params);
                    }
                }else{
                    $flow_entry = $this->_get_entry_user($flow_ids);
                    $users = $this->_get_user_info($flow_entry['entrys']);
                }
            }elseif(isset($role_list[Const_Flow::ROLE_IT])){//IT
                $flow_entry = $this->_get_entry_user($flow_ids);
                $this->_execute_it($flow_entry['entrys']);
                $this->show_tips('OPT_SUCCESS',"/flow/",Const_Tip::SUCCESS);
            }else{
                $this->show_tips('ERROR_PARAM',"/flow/",Const_Tip::ERROR);
            }
            $this->request->set_attribute('users',$users);
            //电脑类型
            $this->request->set_attribute('computer',Const_Flow::computer_type());
            //办公地点
            $address = Const_Flow::office_address();
            $this->request->set_attribute('work_space',$address);
            //错误日志
            $this->request->set_attribute('error',$this->error_item);
            return "Flow_DoList";
        }else{
            $this->show_tips('ERROR_PARAM',"/flow/",Const_Tip::ERROR);
        }
    }
    /**
     * 构建提交数据
     */
    private function _build_post_xz($params){
        $result = array();
        foreach($params['entry_id'] as $key=>$entry_id){
            $result[$entry_id]['computer']   = intval($params['computer'][$key]);
            $result[$entry_id]['work_space'] = trim($params['work_space'][$key]);
            $result[$entry_id]['seat'] = trim($params['seat'][$key]);
            $result[$entry_id]['computer_code'] = trim($params['computer_code'][$key]);
            $result[$entry_id]['office_phone']  = trim($params['office_phone'][$key]);
            $result[$entry_id]['office_phone_ex']  = trim($params['office_phone_ex'][$key]);
            $result[$entry_id]['is_instal']  = $params['is_instal'][$key]?1:0;
        }
        return $result;
    }
    /**
     * 验证数据
     */
    private function _check_post_xz($params){
        $data = $this->_build_post_xz($params);
        $boolean = true;
        foreach($data as $entry_id=>$one){
            //必填项验证
            if(!$one['computer']){
                $this->error_item[$entry_id]['computer'] = '请选择电脑';
                $boolean = false;
            }
            if($one['work_space']==''){
                $this->error_item[$entry_id]['work_space'] = '请选择办公地点';
                $boolean = false;
            }
            if($one['seat']==''){
                $this->error_item[$entry_id]['seat'] = '请输入座位';
                $boolean = false;
            }
            if($one['computer_code']==''){
                $this->error_item[$entry_id]['computer_code'] = '请输入电脑编号';
                $boolean = false;
            }
        }
        return $boolean;
    }
    private function _build_post_xz_error($params){
        $data = $this->_build_post_xz($params);
        $entry_ids = array_keys($data);
        $users = $this->_get_bll_flow_user()->get_user_by_ids($entry_ids);
        $users = $this->_get_user_info($users);
        foreach($users as $key=>$val){

            $users[$key]['is_instal'] = $data[$val['entry_id']]['is_instal'];
            $users[$key]['office_phone_ex'] = $data[$val['entry_id']]['office_phone_ex'];
            if(!isset($this->error_item[$val['entry_id']]['computer'])){
                $users[$key]['computer'] = $data[$val['entry_id']]['computer'];
            }
            if(!isset($this->error_item[$val['entry_id']]['work_space'])){
                $users[$key]['work_space'] = $data[$val['entry_id']]['work_space'];
            }
            if(!isset($this->error_item[$val['entry_id']]['seat'])){
                $users[$key]['seat'] = $data[$val['entry_id']]['seat'];
            }
            if(!isset($this->error_item[$val['entry_id']]['computer_code'])){
                $users[$key]['computer_code'] = $data[$val['entry_id']]['computer_code'];
            }
            if(!isset($this->error_item[$val['entry_id']]['office_phone'])){
                $users[$key]['office_phone'] = $data[$val['entry_id']]['office_phone'];
            }
        }
        return $users;
    }
    /**
     * 取消入职
     */
    private function _entry_cancel($flow_ids){
        if(!is_array($flow_ids) || empty($flow_ids)){
            return;
        }
        $flows = $this->_get_bll_flow()->get_flow_by_ids($flow_ids);
        foreach($flows as $flow){
            $role = array('hr'=>true);
            $params = array('entry_id'=>$flow['user_id'],'submit'=>2);
            $data = array('is_entry'=>Const_Flow::ENTRY_CANCEL);
            $this->_get_bll_flow_user()->operate_entry($role,$params,$data,$this->get_user_id());
        }
    }
    /**
     * 确认入职
     */
    private function _entry_ok($flow_ids){
        if(!is_array($flow_ids) || empty($flow_ids)){
            return;
        }
        $flows = $this->_get_bll_flow()->get_flow_by_ids($flow_ids);
        foreach($flows as $flow){
            $role = array('hr'=>true);
            $params = array('entry_id'=>$flow['user_id'],'submit'=>1);
            $data = array('is_entry'=>Const_Flow::ENTRY_OK);
            $this->_get_bll_flow_user()->operate_entry($role,$params,$data,$this->get_user_id());
        }
    }
    /**
     * 获取所有信息
     */
    private function _get_user_info($users){
        $result = array();
        $citys = $this->_get_bll_hroa()->get_city_list(false);
        foreach($users as $user){
            //job
            $job_dep = $this->_get_bll_hroa()->get_job_dep_info($user['job_id']);
            $user['job_name'] = $job_dep['position_name'];
            $user['city_name'] = $citys[$user['work_city_id']]['city_name'];
            $user['department_name'] = $job_dep['bu_name'].$job_dep['department_name'];

            //leader
            $leader = $this->_get_bll_hroa()->get_user_base_info($user['parent_id']);
            $user['parent_name'] = $leader['real_name'];
            $user['parent_phone']= $leader['office_phone'];
            $user['parent_phone'] .= ($leader['office_phone']&&$leader['office_phone_ex'])?'-':'';
            $user['parent_phone'] .=$leader['office_phone_ex'];

            //date
            $user['re_entry_date'] = $user['re_entry_date']?date('Y-m-d',strtotime($user['re_entry_date'])):date('Y-m-d',strtotime($user['entry_date']));
            $result[] = $user;
        }
        return $result;
    }
    /**
     * it批处理
     */
    private function _execute_it($users){
        if(!is_array($users) || empty($users)){
            return array();
        }
        foreach($users as $user){
            if($user['domain_account'] && $user['office_mail']){
                $role = array('it'=>true);
                $params = array('entry_id'=>$user['entry_id'],'submit'=>3);
                $data = array();
                $this->_get_bll_flow_user()->operate_entry($role,$params,$data,$this->get_user_id());
            }else{//未能处理的

            }
        }
    }
    /**
     * 行政批处理
     */
    private function _execute_xz($users){
        if(!is_array($users) || empty($users)){
            return array();
        }
        foreach($users as $entry_id => $user){
            $role = array('xz'=>true);
            $params = array('entry_id'=>$entry_id,'submit'=>3);
            $data = $user;
            $this->_get_bll_flow_user()->operate_entry($role,$params,$data,$this->get_user_id());
        }
    }
    /**
     * 区分行政还是应该it办理
     */
    private function _execute_it_or_xz($flow_ids){
        $result = array();
        $flow_entry = $this->_get_entry_user($flow_ids);
        foreach($flow_entry['entrys'] as $user){//it可办理
            if($user['domain_account'] && $user['office_mail']){
                $result['it'][]=$user;
            }
        }
        foreach($flow_entry['flows'] as $flow){//行政可办理
            if(($flow['flow_role_list'] == Const_Flow::ROLE_ADMIN && is_numeric($flow['flow_role_list']))||($flow['flow_role_list'] && !is_numeric($flow['flow_role_list']))){
                $result['xz'][] = $flow_entry['entrys'][$flow['user_id']];
            }
        }
        return $result;
    }
    /**
     * 待办理用户
     */
    private function _get_entry_user($flow_ids){
        $result = array();
        if(!is_array($flow_ids) || empty($flow_ids)){
            return $result;
        }
        //flow
        $temp = $this->_get_bll_flow()->get_flow_by_ids($flow_ids);
        $flows = array();
        foreach($temp as $flow){
            $flows[$flow['user_id']] = $flow;
        }
        //entry
        $entry_ids = array_keys($flows);
        $temp = $this->_get_bll_flow_user()->get_user_by_ids($entry_ids);
        $users = array();
        foreach($temp as $user){
            if(!$user['is_entry']){//流程进行中，没有确认入职，也没有取消入职
                $users[$user['entry_id']] = $user;
            }
        }
        return array('flows'=>$flows,'entrys'=>$users);
    }
    /**
     * 入职bll
     * @return Bll_Flow_User
     */
    private function _get_bll_flow_user(){
        if(!$this->bll_flow_user ){
            apf_require_class('Bll_Flow_User');
            $this->bll_flow_user = new Bll_Flow_User();
        }
        return $this->bll_flow_user;
    }
    /**
     * 待办事项
     * @return Bll_Flow_Info
     */
    private function _get_bll_flow(){
        if(!$this->bll_flow ){
            apf_require_class('Bll_Flow_Info');
            $this->bll_flow = new Bll_Flow_Info();
        }
        return $this->bll_flow;
    }
    /**
     * hroa interface bll
     * @return Bll_Hr_Property
     */
    private function _get_bll_hroa(){
        if(!$this->bll_hroa ){
            apf_require_class('Bll_Hr_OutInterface');
            $this->bll_hroa = new Bll_Hr_OutInterface();
        }
        return $this->bll_hroa;
    }
}